package com.extension.company.model.login;

import android.app.Activity;
import android.text.TextUtils;

import com.extension.company.Constant;
import com.extension.company.MyApplication;
import com.extension.company.model.entity.LoginBean;
import com.extension.company.model.entity.UserInfo;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.contract.login.LoginContract;
import com.extension.company.eventbus.WXLoginCode;
import com.extension.company.model.api.RetrofitUrlApi;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 登录的回调接口
 */
public class LoginMode implements LoginContract.Mode {

	private LoginContract.CallBackString callBackString;
	private RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);

	@Override
	public void login(Activity mActivity, final LoginContract.CallBackString callBackToPresent) {
		this.callBackString = callBackToPresent;
		if (!EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().register(this);
		}

//		IWXAPI wxapi = WXAPIFactory.createWXAPI(mActivity,"wx0ee56f81295b58ca" , true);
//		wxapi.registerApp("wx0ee56f81295b58ca");

		SendAuth.Req req = new SendAuth.Req();
		req.scope = "snsapi_userinfo";
		req.state = "wechat_sdk_demo_test";
		MyApplication.iwxapi.sendReq(req);
	}


	/**
	 * 接收微信授权后返回的code 值
	 *
	 * @param wxLoginCode 微信登录返回的code
	 */
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void WXLoginCode(WXLoginCode wxLoginCode) {
		if (!TextUtils.isEmpty(wxLoginCode.getCode())) {
			register(Constant.SIGN, wxLoginCode.getCode());
		}
//		EventBus.getDefault().unregister(this);  // 接收到数据后，就解绑EventBus
	}


	/**
	 * 用户登录
	 *
	 * @param sign 标记
	 * @param code 微信的授权的code值
	 */
	private void register(String sign, String code) {
		retrofitUrlApi.wxLogin(sign, code)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<LoginBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(LoginBean loginBean) {
						if (loginBean.getCode() == 200 && null != loginBean.getData()) {
							String token = loginBean.getData().getUserinfo().getToken();       // 请求的token
							Constant.TOKEN = token;
							UserInfo userInfo = new UserInfo();

							userInfo.setUserName(loginBean.getData().getUserinfo().getNickname());
							userInfo.setMobilePhone(loginBean.getData().getUserinfo().getMobile());
							userInfo.setUserImg(loginBean.getData().getUserinfo().getAvatar());
							userInfo.setGroupId(loginBean.getData().getUserinfo().getGroup_id());
							Constant.userInfo = userInfo;

							callBackString.modelCallBackDataSuccess("");
							if (EventBus.getDefault().isRegistered(this)) {
								EventBus.getDefault().unregister(this);  // 接收到数据后，就解绑EventBus
							}

						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});

	}
}
